<template>
  <div class="dataBase">
    <ContentWrap>
      <el-form label-width="100px" ref="searchRef" :model="queryParams">
        <el-row>
          <el-col :span="6">
            <el-form-item label="标准名称" prop="dataStandardName">
              <ns-input
                v-model="queryParams.dataStandardName"
                clearable
                placeholder="请输入标准名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标准状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="请选择标准状态"
                clearable
                class="!w-240px"
              >
                <el-option label="开启" value="1" />
                <el-option label="关闭" value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="授权状态" prop="ruleStatus">
              <el-select
                v-model="queryParams.ruleStatus"
                placeholder="请选择授权状态"
                clearable
                class="!w-240px"
              >
                <el-option label="已授权" :value="1" />
                <el-option label="未授权" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" class="text-right">
            <el-button @click="searchReset" plain>重置</el-button>
            <el-button @click="search" plain>搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-form-item>
        <el-button type="primary" @click="handleAuthorization(1)"> 授权</el-button>
        <el-button plain @click="handleAuthorization(0)"> 回收权限</el-button>
      </el-form-item>
      <el-table
        v-loading="loading"
        :data="list"
        :default-expand-all="isExpandAll"
        row-key="id"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <el-table-column
          :show-overflow-tooltip="true"
          label="标准名称"
          prop="dataStandardName"
          width="250"
        />
        <!-- <el-table-column :show-overflow-tooltip="true" label="负责人" prop="leaderUserName" /> -->
        <el-table-column label="权限状态" prop="ruleStatusName">
          <template #default="{ row }">
            <div>
              <el-icon v-if="row.ruleStatus == 1"><Check /></el-icon>
              <el-icon v-else><Close /></el-icon>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template #default="scope">
            {{ scope.row.status == '1' ? '开启' : '关闭' }}
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.page"
        :total="total"
        @pagination="getList"
      />
    </ContentWrap>
  </div>
</template>

<script setup lang="tsx">
import { Check, Close } from '@element-plus/icons-vue'
import * as api from '@/api/dataAuthorization'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const route = useRoute()
const total = ref(10) // 列表的总页数
const searchRef = ref()
const isExpandAll = ref(true)
const loading = ref(true) // 列表的加载中
const list = ref<any>([]) // 列表的数据
const queryParams = ref({
  page: 1,
  pageSize: 10,
  dataStandardName: '',
  status: '',
  ruleStatus: ''
})

const searchReset = () => {
  searchRef.value.resetFields()
  search()
}

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
  console.log('multipleSelection.value', multipleSelection.value)
}

const search = () => {
  getList()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const res = await api.standardList({
      ...queryParams.value,
      userId: route.query.id,
      dateStatus: 1
    })
    list.value = res.data
    total.value = res.count
  } finally {
    loading.value = false
  }
}

const handleAuthorization = async (available) => {
  if (multipleSelection.value.length == 0) {
    message.warning('请选择授权标准')
    return
  }
  const params = multipleSelection.value?.map((item) => {
    return {
      userId: route.query.id,
      relevancyId: item.id,
      relevancyType: '3',
      available: available
    }
  })
  console.log('params', params)
  const data = await api.dataAuthorizationUpdate(params)
  console.log('data', data)
  if (data) {
    getList()
    message.success('操作成功')
  }
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss">
.dataBase {
  .el-dropdown-link {
    cursor: pointer;
    color: var(--el-color-primary);
    display: inline-flex;
    align-items: center;
    line-height: 23px;
    padding-left: 12px;
  }
  .el-form .el-select {
    width: 100%;
  }
  :deep(.hide-expand .cell) {
    display: none;
  }
  :deep(.el-form .el-cascader) {
    width: 100%;
  }
  :deep(.operate-btn .el-button.is-text) {
    padding: 0 10px 0 0;
  }
  .el-dropdown-link:focus {
    outline: none;
  }
}
</style>
